
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>

int size_numbers_entered;

   struct global_node
   {
      int value;
      struct global_node *next;
   };
   
struct global_node *first_node=NULL;



struct global_node * display()
{
    struct global_node *p;

    printf("The list is  \n");
    printf("[ \t");

    for(p = first_node; p != NULL ; p = p->next) {
        printf("%d",p->value);
    }
    
    printf("\t ]");
}
   
 struct global_node *add_values(int number_to_add)
{

    struct global_node *new_node;                           //We take a new node having the same instance as that of the struct global_node and make 
                                                           //It point its value to the number to add .  
           
    new_node=malloc(sizeof(struct global_node));
    
    if(new_node==NULL)
    {
       printf("The space is not allocated");
    }
    
    
    new_node -> value = number_to_add;
    new_node -> next = first_node;  
    first_node = new_node;       
    
}

struct global_node * append()

{
       int number_to_append;
       printf("Enter the element you want to append :");
       scanf("%d",&number_to_append);
       first_node = add_values(number_to_append);
       display();
       
}



// The only difference between append and extend function is the number of elements you are inputing

struct global_node * extend ()
{
      int number,num,i;
      
      printf("Enter the number of elements you want to extend \n");
      scanf("%d",&number);
      printf("Enter the elements \n");
      
      for(i=0;i<number;i++)
      {
          scanf("%d",&num);
          first_node=add_values(num);
      }
      display();
}

struct global_node *search()

{
    int number;
    struct global_node *pointer_used_to_find_the_element  ; 
    
    printf("Enter the element you want you search \n ");
    scanf("%d",&number);
    pointer_used_to_find_the_element=first_node;
    
    for(pointer_used_to_find_the_element=first_node; pointer_used_to_find_the_element!=NULL; pointer_used_to_find_the_element=pointer_used_to_find_the_element->next){
       if(pointer_used_to_find_the_element -> value == number){
           
           printf("The element you were searching for exists in the list \n ");
   }
}
}





struct global_node * pop()
{
    int index_number,count=0;
    struct global_node *previous,*current;
    
    printf("Enter the index of the element u want to remove  \n ");
    scanf("%d",&index_number);
    
    count = index_number; 
    
    for(previous = NULL, current = first_node; current!=NULL  ;previous = previous -> next,current = current->next) {
      count=count+1;
    }
    if(current == NULL)
    {
       printf("The element you are looking for does not exist in the list \n");
    }
    if(current->value == index_number)
        { 
           previous -> next = current->next;
        }
    free(previous);
    display();
 }

struct global_node *remove_func()
{
        int number;
        struct global_node *previous,*current;
        
        printf("Enter the element you want to remove \n");
        scanf("%d",&number);
        
        previous = NULL;
        current = first_node;
        
        for(; current!=NULL && current -> value != number ; previous = previous -> next, current= current->next)
        {
        if(current==NULL){
            return first_node;
        }
        if(previous==NULL){
           first_node = first_node->next;
        }
        else            { 
           previous->next = current->next;
        }
        free(previous);
        display();   
     }
}


struct global_node * insert()
{
    struct global_node *p,*q;
    int s,count=0;
    printf("enter the position where u want to insert : ");
    scanf("%d",&s);
    for(p=NULL,q=first_node;q!=NULL;p=p->next,q=q->next)
    {
       count=count+1;
        if(count==s)
        {
        }
    }
}     

struct global_node * indexx()
{
    int count=0,number_whose_frequency;
    struct global_node *p;
    
    printf("enter the element whose index u want to find : ");
    scanf("%d",&number_whose_frequency);
    
    for( p = first_node;p != NULL && p->value != number_whose_frequency; p = p->next)
    {
       count=count+1;  
    }
    if(p == NULL)
    {
       printf("Element not found\n");
    }
    if(p -> value == number_whose_frequency)
    {
       printf("The index of the element you are looing for is  %d ",count);
    }
}





 

struct global_node * count_func()
{      
 int count = 0;
 struct global_node *p;
 
	p=first_node;
	while(p != NULL)
	{   count++;
		 p=p->next;
	}
}

void list_size()
{
printf("Your list is of size %d",size_numbers_entered);
}

struct global_node * reverse()
{
struct global_node *p;

    printf("The list is  \n");
    printf("[ \t");

    for(p = NULL; p != first_node ; p = p->next) {
        printf("%d",p->value);
    }
    
    printf("\t ]");
}


struct  global_node * check_value_in_list()
{	
        int number;
        printf("Enter the number whose value you want to check");
	scanf("%d",&number);
	for(;first_node != NULL; first_node = first_node->next )
	{	
		if(first_node -> value == number)
			printf("The number entered exists in the list");
	}
}


struct global_node *sort_func()

{
  int i,j,temp;
 struct global_node *current, *previous, *p;
 
  for(i=1 ,p = first_node  ; i < size_numbers_entered ;  p = p->next , ++i) {
  
    for( j=0 , previous = first_node ; j < size_numbers_entered-i ;  previous = previous->next , ++j)
        if(previous -> value > (previous->next)->value)
       {
	 temp = previous->value;
         previous->value = (previous->next)->value;
         (previous->next)->value = temp;
       }
}
}

int main()
{
   
   int option,i,number_input;

   
   printf("Enter the number of elements you want to enter :");
   scanf("%d",&size_numbers_entered);
  
  
   for(i = 0;i<size_numbers_entered; i++)
   {
       printf("Enter an number\t");
       scanf("%d", &number_input);      
       first_node = add_values(number_input);
   }
   
   while(1)
   {
   
   printf("Your list is of size \n %d",size_numbers_entered);
   
   printf("Your list is  \n");
   display();
   
   printf(" Which function do you want to implement - \n ");
   printf(" \n------------------------------------------------------------------------------------------ \n");
   printf("       	           0)        Display\n");
   printf("                   1)        Append\n");
   printf("                   2)        Extend\n");
   printf("                   3)        Pop\n");
   printf("                   4)        Remove\n");
   printf("                   5)        Index\n");
   printf("                   6)        lise_size\n");
   printf("                   7)        sort\n");
   printf("                   8)        reverse\n");
   printf("                   9)        count\n");
   printf("                   10        insert\n");
   printf("                   11)       Check if value in list\n");
   printf("                   12)       Add more values\n");
   printf("                   13)       Exit\n");
		
		printf
		    (" \n    ------------------------------------------------------------------------------------------   \n  ");

		printf("\t\t\t\t Please enter an option \t");
   scanf("%d",&option);


   switch(option)
   {
       case 1 :append();
               break;
       case 2 :extend();
               break;
       case 3 :pop();
               break;
       case 4 :remove_func();
               break;
       case 5 :indexx();
               break;
       case 6 :list_size();
               break;
       case 7 :sort_func();
               break;
       case 8 :reverse();
               break;
      case 9 :count_func();
                break; 
      case 10 :insert();
                break; 
      case 11 :check_value_in_list();
                break; 
      case 12 :append();
                break; 
      case 13 :exit(0); 

      }

}
}
